package com.linyx.simpleboot.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.PostConstruct;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.linyx.simpleboot.zookeeper.LeaderLatch.MyLeaderLatch;

import ch.qos.logback.core.joran.conditional.IfAction;

//@Component
public class ScheduledTasks {

	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	
	@Autowired
	private Environment env;

	@Autowired
	private MyLeaderLatch myLeaderLatch;

//	@Scheduled(cron="/5")
	public void reportCurrentTime() throws InterruptedException {
		log.info("The time is now {}", dateFormat.format(new Date()));
		
		
		
		if(myLeaderLatch.hasLeadership()) {
			log.info("获得领导节点");
//			Thread.sleep(100000);
		}
		
//		myLeaderLatch.close();
		if(!myLeaderLatch.hasLeadership()) {
		log.info("放弃领导节点");
		}
		 
	}
	
	@PostConstruct
	public void testEnvironment() {
		
		System.out.println("容器初始化的时候执行");
		System.out.println("获取环境变量："+env.getProperty("mybaby"));
		System.out.println("获取环境变量："+env.getProperty("logging.level.root"));
		System.out.println("获取本地config： "+env.getProperty("test"));
		System.out.println("获取jar环境变量："+env.getProperty("error.msg.DoBusinessError"));
		
 	}
}